﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices;

namespace AmiBrokerPlugin
{
    internal static class Extensions
    {
        internal static void UpdateStatus(this IntPtr ptr, StatusCode code, Color color, string shortMessage, string fullMessage)
        {
            Marshal.WriteInt32(new IntPtr(ptr.ToInt32() + 4), (int)code);
            Marshal.WriteInt32(new IntPtr(ptr.ToInt32() + 8), color.R);
            Marshal.WriteInt32(new IntPtr(ptr.ToInt32() + 9), color.G);
            Marshal.WriteInt32(new IntPtr(ptr.ToInt32() + 10), color.B);

            var enc = Encoding.GetEncoding("windows-1251");
            
            var msg = enc.GetBytes(fullMessage);
            for (int i = 0; i < (msg.Length > 255 ? 255 : msg.Length); i++)
            {
                Marshal.WriteInt32(new IntPtr(ptr.ToInt32() + 12 + i), msg[i]);
            }
            Marshal.WriteInt32(new IntPtr(ptr.ToInt32() + 12 + msg.Length), 0x0);

            msg = enc.GetBytes(shortMessage);
            for (int i = 0; i < (msg.Length > 31 ? 31 : msg.Length); i++)
            {
                Marshal.WriteInt32(new IntPtr(ptr.ToInt32() + 268 + i), msg[i]);
            }
            Marshal.WriteInt32(new IntPtr(ptr.ToInt32() + 268 + msg.Length), 0x0);
        }
    }
}
